<template>
  <div class="">
    <div class="view">
      <div class="editTop">
        <el-button
          icon="el-icon-back"
          circle
          type="primary"
          @click="closeEditModal"
        ></el-button>
        <el-button
          type="primary"
          @click="createEditNewAd"
          style="margin-left:30px"
          >新建标签</el-button
        >
      </div>
      <el-table :data="tableData" border>
        <el-table-column prop="name" label="标签名称"></el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag :type="scope.row.status ? 'success' : 'info'">{{
                adStatus[scope.row.status]
              }}</el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button
              @click="createEditNewAd(scope.row, true)"
              type="text"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              @click="handleCurrentStatus(scope.row)"
              type="text"
              size="small"
            >
              {{ scope.row.status ? "下架" : "上架" }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="`${isEdit ? '编辑' : '新建'}资讯分类`"
      :visible.sync="editModalVisible"
      size="mini"
      width="400"
    >
      <el-form
        label-position="right"
        label-width="120px"
        :model="originForm"
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="originForm.name" style="width: 200px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editModalVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { adStatus } from "@/config/ad";

export default {
  props: ["closeEditModal"],
  data() {
    return {
      adStatus,
      formInline: {
        user: "",
        region: "",
      },
      tableData: [],
      // 表单相关
      isEdit: false,
      editModalVisible: false,
      originForm: {
        name: "",
        type: 'BQ',
        status: 1
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    createEditNewAd(config, isEdit) {
      this.isEdit = isEdit;
      this.originForm = isEdit ? config : this.originForm;
      this.editModalVisible = true;
    },
    handleCurrentStatus(config) {
      this.$confirm(
        `是否确认 ${config.status ? "下架" : "上架"} 资讯标签 [${config.name}]?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$axios
          .put(
            `/api/tags/${config.uuid}.json?_method=put`,
            { status: Number(!config.status) }
          )
          .then((response) => {
            this.$message.success("操作成功");
            this.getArticleTypeList();
          });
      });
    },
    handleSubmit() {
      const {originForm, isEdit} = this
      const ajaxType = isEdit ? 'put' : 'post'
      const url = isEdit ? `/api/tags/${originForm.uuid}.json?_method=put` : '/api/tags.json'
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.$axios[ajaxType](url, this.originForm).then((response) => {
            this.$message.success("提交成功");
            this.getArticleTypeList();
            this.editModalVisible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getArticleTypeList() {
      const params = {
        type: "BQ",
      };
      this.$axios.get("/api/tags.json", { params }).then((response) => {
        this.tableData = response.list;
        this.originForm = {
          name: "",
          type: 'BQ'
        }
      });
    },
  },
  created() {
    this.getArticleTypeList();
  },
};
</script>
<style lang="scss" scoped>
.view {
  margin: 0 20px;
}
.editTop {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
  // margin-left: 20px;
  button {
    margin-right: 10px;
  }
}
</style>
